package com.qianyu.project.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianyu.project.core.annotation.FlowBegin;
import com.qianyu.project.core.annotation.FlowEntry;
import com.qianyu.project.entity.AttachemenInfo;
import com.qianyu.project.entity.DeviceCollect;
import com.qianyu.project.entity.DeviceRepair;
import com.qianyu.project.entity.DeviceScrap;
import com.qianyu.project.mapper.DeviceCollectMapper;
import com.qianyu.project.mapper.DeviceRepairMapper;
import com.qianyu.project.mapper.DeviceScrapMapper;
import com.qianyu.project.service.IAttachemenInfoService;
import com.qianyu.project.service.IDeviceRepairService;
import com.qianyu.project.service.IDeviceScrapService;
import com.qianyu.project.utils.PageUtil;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 证件费用表 服务实现类
 * </p>
 *
 * @author gs
 * @since 2021-01-30
 */
@Service
@Transactional
public class DeviceRepairServiceImpl extends ServiceImpl<DeviceRepairMapper, DeviceRepair> implements IDeviceRepairService {

    @Autowired
    private DeviceRepairMapper deviceRepairMapper;
    @Autowired
    private IAttachemenInfoService attachemenInfoService;

    @Override
    @FlowBegin("device_repair")
    public Integer Mysave(@FlowEntry("id") DeviceRepair dc, Map<String, Object> params) {
        deviceRepairMapper.insert(dc);
        List<Map<String, Object>> mapEs = (List<Map<String, Object>>) params.get("enclosures");
        List<AttachemenInfo> aiList = Lists.newArrayList();

        mapEs.forEach(i->{
            AttachemenInfo ai = new AttachemenInfo();
            ai.setFileName(i.get("fileName")+"");
            String fileName = i.get("fileName") + "";
//            ai.setFileType(fileName.split(".")[1]);
            ai.setFilePath(i.get("filePath")+"");
            ai.setUploadDate(i.get("time")+"");
            ai.setRelativeId(dc.getId());
            aiList.add(ai);
        });
        List<Integer> Allid = Lists.newArrayList();
        attachemenInfoService.saveBatch(aiList);
        aiList.forEach(i->{
            Allid.add(i.getId());
        });
        if(aiList.size()!=0){
            String attid = "";
            for (int i = 0; i< Allid.size(); i++){
                if (Allid.size()-1==i){
                    attid+=Allid.get(i);
                    continue;
                }
                attid+=Allid.get(i)+",";
            }
            DeviceRepair d = new DeviceRepair();
            d.setId(dc.getId());
            d.setAttachIds(attid);
            deviceRepairMapper.updateById(d);
        }


        return 1;
    }

    @Override
    public void Myupdate(DeviceRepair dc, Map<String, Object> params) {
        List<Map<String, Object>> mapEs = (List<Map<String, Object>>) params.get("enclosures");
        List<AttachemenInfo> aiList = Lists.newArrayList();
        mapEs.forEach(i->{
            AttachemenInfo ai = new AttachemenInfo();
            ai.setId(Integer.valueOf(i.get("id")+""));
            ai.setFileName(i.get("fileName")+"");
            String fileName = i.get("fileName") + "";
//            ai.setFileType(fileName.split(".")[1]);
            ai.setFilePath(i.get("filePath")+"");
            ai.setUploadDate(i.get("time")+"");
            ai.setRelativeId(dc.getId());
            aiList.add(ai);
        });
        attachemenInfoService.saveOrUpdateBatch(aiList);
    }

    @Override
    public IPage<DeviceRepair> Mylist(Map<String, String> params) {
        IPage<DeviceRepair> page = PageUtil.createPage(params);
        return deviceRepairMapper.Mylist(page,params);
    }

    @Override
    public List<DeviceRepair> listNopage(Map<String, String> params) {
        return deviceRepairMapper.listNopage(params);
    }
}
